package ca.etsmtl.gti785.util;

import java.util.List;
import ca.etsmtl.gti785.R;
import ca.etsmtl.gti785.classes.Fichier;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This class is used to adapt a list of files to an android list.
 * @see Fichier
 * @author Etienne Faucher
 */
public class FileAdapter extends ArrayAdapter<Fichier> 
{
	public FileAdapter(Context context, int resource,List<Fichier> objects) 
	{
		super(context, resource, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
	    View v = convertView;

	    if (v == null) 
	    {
	        LayoutInflater vi;
	        vi = LayoutInflater.from(getContext());
	        v = vi.inflate(R.layout.file_list_item_layout, null);
	    }

	    Fichier p = getItem(position);
	    if (p != null) 
	    {
	        TextView fileName = (TextView) v.findViewById(R.id.FileName);
	        if (fileName != null) 
	        {
	        	fileName.setText(p.name);
	        }
	        
	        ImageView iv = (ImageView) v.findViewById(R.id.icon);
	        if(iv != null) 
	        { 
	        	if(p.isFolder)
	        	{
		        	iv.setImageResource(R.drawable.folder);
	        	}
	        	else
	        	{
		        	iv.setImageResource(R.drawable.file);
	        	}
	        }
	    }
	    return v;
	}
}
